<?php
defined('BASEPATH') OR exit("No direct script access allowed");
class Ssh {
    private $host='localhost';
    private $user='root';
    private $port='36000';
    private $password='password';
    private $con=null;
    private $shell_cmd="";
    private $log='';
    function __construct($host='',$port='',$passwd='',$user='',$cmd=''){

        if ($host!='')
            $this->host = $host;
        if ($port!='')
            $this->port = $port;
        if ($passwd!='')
            $this->password = $passwd;
        if ($user!='')
            $this->user = $user;
        if ($cmd!='')
            $this->shell_cmd = $cmd;
        $this->con = ssh2_connect($this->host,$this->port);
        if (!$this->con) {
            $this->log .= sprintf("%s Connect failed!",$this->host);
        }
    }
    function authPassword( $user='',$passwd='' ) {
        if($user!='')
            $this->user = $user;
        if($passwd!='')
            $this->password=$passwd;
        if( !ssh2_auth_password($this->con,$this->user,$this->password )){
            $this->log .= sprintf("%s Authorization failed!",$this->host);
        }
    }
    function cmdExec() {
        $stream = ssh2_exec($this->con,$shell_cmd);
        stream_set_blocking($stream,true);
        return fread($stream,4096);
    }
    function getLog(){
        return $this->log;
    }
}
